package mall

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/repository"
	"mall-go/response"
	"strconv"
)

type ShopCartController struct {
	DB *gorm.DB
}

// CartItemPageList 分页获取购物车信息
//todo 暂时无法测试
func (s ShopCartController) CartItemPageList(ctx *gin.Context) {
	//获取用户信息

	user, _ := ctx.Get("mallUser")
	str := ctx.Query("pageNumber")
	pageNumber, _ := strconv.Atoi(str)
	if pageNumber < 1 {
		pageNumber = 1
	}
	cartRepo := repository.NewShoppingCartRepo()

	pageDTO := dto.ShopCartPageDTO{
		UserId: user.(model.MallUser).UserID,
		PageDTO: dto.PageDTO{
			Page:  pageNumber,
			Limit: 5,
			Start: (pageNumber - 1) * 5,
		},
	}
	items := cartRepo.GetMyShoppingCartItems(pageDTO)
	response.Success(ctx, gin.H{
		"pageResult": items,
	}, "Success")
}

// CartItemList 购物车列表(网页移动端不分页)
func (s ShopCartController) CartItemList(ctx *gin.Context) {
	//获取用户信息
	user, _ := ctx.Get("mallUser")
	userId := user.(model.MallUser).UserID
	cartRepo := repository.NewShoppingCartRepo()
	cartItemVOS := cartRepo.GetMyShoppingCartItemsByUserId(int(userId))
	response.Success(ctx, gin.H{"cartItemVOS": cartItemVOS}, "Success")
}

// SaveNewBeeMallShoppingCartItem 添加商品到购物车
func (s ShopCartController) SaveNewBeeMallShoppingCartItem(ctx *gin.Context) {
	cartRepo := repository.NewShoppingCartRepo()
	//获取用户id
	user, _ := ctx.Get("mallUser")
	userId := user.(model.MallUser).UserID
	//获取请求参数
	var request dto.CartRequest
	ctx.Bind(&request)
	str := cartRepo.SaveNewBeeMallCartItem(request, int(userId))
	if str == "Success" {
		response.Success(ctx, nil, "添加成功")
	} else {
		response.Failure(ctx, nil, str)
	}
}

// UpdateNewBeeMallShoppingCartItem 修改购物车数据
func (s ShopCartController) UpdateNewBeeMallShoppingCartItem(ctx *gin.Context) {
	cartRepo := repository.NewShoppingCartRepo()
	//获取用户id
	user, _ := ctx.Get("mallUser")
	userId := user.(model.MallUser).UserID
	//获取请求参数
	var request dto.CartRequest
	ctx.Bind(&request)
	str := cartRepo.UpdateNewBeeMallCartItem(request, int(userId))
	if str == "Success" {
		response.Success(ctx, nil, "修改成功")
	} else {
		response.Failure(ctx, nil, str)
	}
}

func NewShopCartController() ShopCartController {
	db := common.GetDB()
	db.AutoMigrate(model.ShoppingCart{})
	return ShopCartController{DB: db}
}

// ToSettle 获取结算单页面详情
func (s ShopCartController) ToSettle(ctx *gin.Context) {
	cartRepo := repository.NewShoppingCartRepo()
	//获取userId
	user, _ := ctx.Get("mallUser")
	userId := user.(model.MallUser).UserID
	cartItemIds := ctx.Query("cartItemIds")
	if len(cartItemIds) < 1 {
		response.Failure(ctx, nil, "参数异常")
		return
	}
	priceTotal := 0
	itemsForSettle := cartRepo.GetCartItemsForSettle(cartItemIds, userId)
	if len(itemsForSettle) == 0 {
		//没有数据
		panic("参数异常")
	} else {
		//总价
		for _, vo := range itemsForSettle {
			priceTotal += vo.GoodsCount * vo.SellingPrice
		}
		if priceTotal < 1 {
			panic("售价异常")
		}
	}
	response.Success(ctx, gin.H{"itemsForSettle": itemsForSettle}, "Success")
}

// DeleteNewBeeMallShoppingCartItem 删除购物车数据
func (s ShopCartController) DeleteNewBeeMallShoppingCartItem(ctx *gin.Context) {
	newBeeMallShoppingCartItemId, _ := strconv.Atoi(ctx.Params.ByName("newBeeMallShoppingCartItemId"))
	user, _ := ctx.Get("mallUser")
	userId := user.(model.MallUser).UserID
	cartRepo := repository.NewShoppingCartRepo()
	newBeeMallCartItemById := cartRepo.GetNewBeeMallCartItemById(newBeeMallShoppingCartItemId)
	if newBeeMallCartItemById.CartItemId == 0 {
		response.Failure(ctx, nil, "数据不存在")
		return
	}
	if userId != newBeeMallCartItemById.UserId {
		response.Failure(ctx, nil, "非法操作")
		return
	}
	//执行删除操作
	ok := cartRepo.DeleteById(newBeeMallShoppingCartItemId, userId)
	if ok {
		response.Success(ctx, nil, "删除成功")
	} else {
		response.Failure(ctx, nil, "删除失败")
	}
}
